package com.example.optimizaapp.domain.Utilities;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.example.optimizaapp.LoginActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class Webservices{

	private String urlWebservice;
		
	public Webservices(){
		super();
	}
	
			
	public String getUrlWebservice() {
		return urlWebservice;
	}

	public void setUrlWebservice(String urlWebservice) {
		this.urlWebservice = urlWebservice;
	}

	public static String callWebService (String url){
		Log.d("MESSAGE =======>", "LLAMANDO SERVICIO A URL: " + url);
		try{
			HttpClient httpclient = new DefaultHttpClient();
			Uri myUri = Uri.parse(url);
			System.out.println("PARSED URI =========>" + myUri.toString());
			HttpGet request = new HttpGet(myUri.toString());
			HttpParams httpParameters = new BasicHttpParams();
			// Set the timeout in milliseconds until a connection is established.
			// The default value is zero, that means the timeout is not used. 
			int timeoutConnection = 3000;
			HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
			// Set the default socket timeout (SO_TIMEOUT) 
			// in milliseconds which is the timeout for waiting for data.
			int timeoutSocket = 5000;
			HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
		    HttpResponse response = httpclient.execute(request);
		    StatusLine statusLine = response.getStatusLine();
		    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
		        ByteArrayOutputStream out = new ByteArrayOutputStream();
		        response.getEntity().writeTo(out);
		        String responseString = out.toString();
		        out.close();
		        Log.d("RESPONSE >>>>>>>>>>>>>",responseString);
		        return responseString;  
		    } else{
		        //Closes the connection.
		        response.getEntity().getContent().close();
		        throw new IOException(statusLine.getReasonPhrase());
		    } 
		} catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();
            
        }  catch (Exception e){
        	Log.d("EXCEPTION", e.getClass().toString());
        	
        }
		return null;
	}
	
	
	
}
